
package com.hmsg.health.dao;

import com.hmsg.health.bean.domain.TaskDo;
import com.hmsg.health.bean.domain.UserDo;
import com.hmsg.health.bean.domain.UserTaskDo;

import java.util.Date;
import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface UserTaskDao {
    @Select("select * from s_user_task where id=#{id}")
    UserTaskDo findOne(@Param("id")Integer id);
    
    @Select("<script>" +
    	    "select * from  `s_user_task` where 1=1 " +
    	    "    <if test='info.id != null'>" +
    	    "     and `id` = #{info.id} " +
    	    "    </if>" +
    	
    	    "    <if test='info.status != null'>" +
    	    "    and  `status` = #{info.status} " +
    	    "    </if>" +
    	    "    <if test='info.verifyDetail != null'>" +
    	    "    and  `verify_detail` = #{info.verifyDetail} " +
    	    "    </if>" +
    	    "    <if test='info.startTime != null'>" +
    	    "      and  a.create_time <![CDATA[>=]]>#{info.startTime} " +
    	    "    </if>" +
    	    "    <if test='info.endTime != null'>" +
    	    "      and  a.create_time <![CDATA[>=]]>#{info.endTime} " +
    	    "    </if>" +
    	    "    <if test='info.taskId != null'>" +
    	    "      and `task_id` = #{info.taskId}  " +
    	    "    </if>" +
    	    "    <if test='info.userId != null'>" +
    	    "      and `user_id` = #{info.userId}  " +
    	    "    </if>" +
    	    "    order by id desc" +
    	    "    <if test='info.startNo != null'>" +
    	    "      limit #{info.startNo},#{info.pageSize}" +
    	    "    </if>" +
    	    "</script>")
    @Results({
        @Result(column="user_id",property="user",one=@One(select="com.hmsg.health.dao.UserDao.select")),
        @Result(column="task_id",property="task",one=@One(select="com.hmsg.health.dao.TaskDao.findOne"))
    })
    List<UserTaskDo> select(@Param("info") UserTaskDo info);
    
    @Delete("delete from s_user_task where id=#{userId}")
    int delete(Integer userId);
    
  
    @Insert("<script>" +
    "insert into `s_user_task` " +
    "  <set>" +
    "    <if test='info.taskId != null'>" +
    "     `task_id` = #{info.taskId}, " +
    "    </if>" +
    "    <if test='info.userId != null'>" +
    "      `user_id` = #{info.userId}, " +
    "    </if>" +
    "    <if test='info.status != null'>" +
    "      `status` = #{info.status}, " +
    "    </if>" +
    "    <if test='info.verifyDetail != null'>" +
    "      `verify_detail` = #{info.verifyDetail}, " +
    "    </if>" +
    "    <if test='info.createTime != null'>" +
    "      `create_time` = #{info.createTime} " +
    "    </if>" +
    "  </set>" +
    "</script>")
	@Options(useGeneratedKeys = true, keyProperty = "id")
	int save(@Param("info") UserTaskDo info);


    @Update({"<script>",
		"UPDATE  s_user_task",
			"<trim prefix='set' suffixOverrides=','>",
		
		"<if test='user.status!=null'>",
		"status= #{user.status},",
		"</if>",
		"<if test='user.verifyDetail!=null'>",
		"verify_detail= #{user.verifyDetail},",
		"</if>",
		"<if test='user.nopassReason!=null'>",
		"npass_reason= #{user.nopassReason},",
		"</if>",
		"<if test='user.nopassImg!=null'>",
		"nopass_img= #{user.nopassImg}",
		"</if>",
		"</trim>",
		"where id = #{user.id}",
		"</script>"})
 	int update(@Param("user") UserTaskDo user);


    @Select("select count(id) from s_user_task where task_id=#{id} and `status`=4")
    Integer findFailCount(@Param("id") Integer id);


	@Select("select count(id) from s_user_task where task_id=#{id} and `status`=3")
	Integer finishCount(@Param("id") Integer id);
}